package model

import (
	"gitee.com/wydcn/go-tools/conv/bytes"
)

type PayLoad [] byte

func (p *PayLoad) LoadPayLoad(o *ConnectOption){
	// Append the Client Identifier to the payload.

	*p = appendPayLoad(*p,o.ClientID)

	//Append the Will Topic and the Will Message to the payload
	//if the Packet has them.
	if o.WillFlag() {
		*p = appendPayLoad(*p,o.WillTopic)
		*p = appendPayLoad(*p,o.WillMessage)
	}

	// Append the User Name to the payload if the Packet has it.
	if len(o.UserName) > 0 {
		*p = appendPayLoad(*p,o.UserName)
	}

	// Append the Password to the payload if the Packet has it.
	if len(o.Password) > 0 {
		*p = appendPayLoad(*p,o.Password)
	}
}

func appendPayLoad(payLoad []byte, data []byte) []byte{
	payLoad = append(payLoad, bytes.Uint16ToBytes(uint16(len(data)))...)
	payLoad = append(payLoad, data...)
	return payLoad
}
